//
//  XQBaseIdentityCell.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/9.
//

import UIKit

class XQBaseIdentityCell: UITableViewCell,UITextFieldDelegate {
    
    private lazy var bgView : UIView = {
        let view = UIView()
        view.layer.cornerRadius = 12
        view.backgroundColor = UIColor(hex: "#F6F6F6")
        return view
    }()
    
    private lazy var textField : UITextField = {
        let textField = UITextField()
        textField.returnKeyType = .done
        textField.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        textField.delegate = self
        textField.textColor = .xq_black
        textField.keyboardType = .asciiCapable        
        textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return textField
    }()
    
    private lazy var arrowImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "public_accure_arrow")
        imageView.sizeToFit()
        imageView.isHidden = true
        imageView.setContentHuggingPriority(.required, for: .horizontal)
        imageView.setContentCompressionResistancePriority(.required, for: .horizontal)
        return imageView
    }()
    
    private lazy var button : UIButton = {
        let btn = UIButton(type: .custom)
        btn.isHidden = true
        btn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        return btn
    }()
    
    var content : String = "" {
        didSet {
            self.textField.text = content
        }
    }
    
    var placeHolder : String? = "" {
        didSet {
            let attributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.systemFont(ofSize: 15),
                .foregroundColor: UIColor(hex: "#A3A6A9")!
            ]
            textField.attributedPlaceholder = NSAttributedString(string: placeHolder ?? "", attributes: attributes)
        }
    }
    
    var textType : String? = "" {
        didSet {
            if (textType == "whitea" || textType == "whitec") {
                arrowImageView.isHidden = false
                button.isHidden = false
            }else if (textType == "whiteb") {
                arrowImageView.isHidden = true
                button.isHidden = true
            }
        }
    }
    
    var keyBoard : Bool = false {
        didSet {
            self.textField.keyboardType = keyBoard ? .numberPad : .asciiCapable
        }
    }
    
    var textFielHandle: ((String) -> Void)?
    
    var btnHandle:(() -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        makeUI()
        makeConstraints()
        addFinishTool()
    }

    required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        self.contentView.addSubview(self.bgView)
        self.bgView.addSubview(self.textField)
        self.bgView.addSubview(self.arrowImageView)
        self.bgView.addSubview(self.button)
    }
    
    private func makeConstraints() {
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(17)
            make.right.equalTo(-17)
            make.bottom.equalTo(0)
        }
        self.arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
        }
        self.textField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        self.button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    private func addFinishTool() {
        let toolbar = UIToolbar()
        toolbar.sizeToFit()
        let doneButton = UIBarButtonItem(title: "Finish", style: .done, target: self, action: #selector(doneButtonTapped))
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        
        toolbar.setItems([flexibleSpace, doneButton], animated: false)
        
        textField.inputAccessoryView = toolbar
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    @objc private func textFieldDidChange(_ textField: UITextField) {
        textFielHandle?(textField.text ?? "")
    }
    
    @objc private func buttonClick() {
        btnHandle?()
    }
    
    @objc private func doneButtonTapped() {
        self.textField.resignFirstResponder()
    }
}
